<?php

function encode2($str) {
    return base64_encode($str);
}

function decode2($str) {
    return base64_decode($str);
}

function filter2($str) {
    $str = str_replace("【PisDota第一视角】", "", $str);
    $str = str_replace("【PisDOTA第一视角】", "", $str);
    $str = str_replace("【", "", $str);
    $str = str_replace("】", "", $str);
    return $str;
}

function getrss2($url) {
    $regstr = "";
    $regstr .= "/";
    $regstr .= "<li class=\"v_link\"><a title=\"(.*)\" target=\"_blank\" href=\"http:\/\/v.youku.com\/v_show\/id_(.*).html\"><\/a><\/li>";
    $regstr .= ".*";
    $regstr .= "<li class=\"v_thumb\"><img.*src=\"(.*)\".*\/><\/li>";
    $regstr .= ".*";
    $regstr .= "<li class=\"v_time\"><span class=\"num\">(.*)<\/span><span class=\"bg\"><\/span><\/li>";
    $regstr .= "/U";
    $arr = array();
    for ($i = 1; $i < 5; $i++) {
        $url = "http://i.youku.com/u/profile/type_video/order_1_view_1_id_{$url}_page_{$i}";
        $opts = array('http' => array('method' => "GET", 'header' => "Content-Type: text/html; charset=utf-8", 'timeout' => 30));
        $context = stream_context_create($opts);
        $html = file_get_contents($url, false, $context);
        $html = str_replace("\r\n", "", $html);
        $html = str_replace("\n", "", $html);

        $title = array();
        preg_match_all($regstr, $html, $title);

        if (empty($title[0])) {
            break;
        } else {
            for ($k = 0; $k < count($title[0]); $k++) {
                $arr[0][] = filter2($title[1][$k]);
                $arr[1][] = $title[2][$k];
                $arr[2][] = $title[3][$k];
                $arr[3][] = $title[4][$k];
                $arr[4][] = "id_" . encode2("{$title[2][$k]}&{$title[1][$k]}") . ".html";
            }
        }
    }
    //print_r($arr);
    return $arr;
}

function getrss2write($url) {
    $arr = getrss2($url);
    if (count($arr[0]) < 28 - 1) {
        return;
    }
    $k = fopen("index.html", "w");
    fwrite($k, "<!DOCTYPE html>\r\n");
    fwrite($k, "<html>\r\n");
    fwrite($k, "<head>\r\n");
    fwrite($k, "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
    fwrite($k, "<link type=\"text/css\" rel=\"stylesheet\" href=\"main.css\"/>\r\n");
    fwrite($k, "<title>YaphetS,Pis第一视角,Pis优酷空间</title>\r\n");
    fwrite($k, "</head>\r\n");
    fwrite($k, "<body>\r\n");

    fwrite($k, "<div class=\"grid4wimg\">.\r\n");
    fwrite($k, "<div class=\"grid4wH1\">【PisDOTA第一视角】</div>\r\n");
    fwrite($k, "<div class=\"grid4w\">\r\n");
    fwrite($k, "<div class=\"collgrid4w\">\r\n");
    fwrite($k, "<div class=\"items\">\r\n");
    for ($i = 0; $i < count($arr[0]); $i++) {
        fwrite($k, "<ul class=\"v\">\r\n");
        fwrite($k, "<li class=\"v_link\"><a title=\"{$arr[0][$i]}\" target=\"_blank\" href=\"{$arr[4][$i]}\"></a></li>\r\n");
        fwrite($k, "<li class=\"v_thumb\"><img src=\"{$arr[2][$i]}\" alt=\"{$arr[0][$i]}\" title=\"{$arr[0][$i]}\"/></li>\r\n");
        fwrite($k, "<li class=\"v_time\"><span class=\"num\">{$arr[3][$i]}</span><span class=\"bg\"></span></li>\r\n");
        fwrite($k, "<li class=\"v_title\"><a title=\"{$arr[0][$i]}\" target=\"video\" href=\"{$arr[4][$i]}\">{$arr[0][$i]}</a></li>\r\n");
        fwrite($k, "</ul>\r\n");
        if (($i + 1) % 4 == 0 && $i > 0) {
            fwrite($k, "<div class=\"clear\"></div>\r\n");
        }
    }



    fwrite($k, "</div>\r\n");
    fwrite($k, "</div>\r\n");
    fwrite($k, "</div>\r\n");
    fwrite($k, "</div>\r\n");
    fwrite($k, "</body>\r\n");
    fwrite($k, "</html>\r\n");
    fclose($k);
}

//header('Content-Type:text/html;charset=UTF-8');
getrss2write("UMzcyMTMxODQ4");
//echo base64_decode("WE16UTVOalV4TmpJdybjgJBQaXNET1RB56ys5LiA6KeG6KeS44CR5pqX5b2x5oG26a2U");
?>